import 'package:enterprise_v6_app/app/components/custom_modele/custom_row_title.dart' as car;
import 'package:enterprise_v6_app/app/components/custom_modele/custom_search_enter.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/dialog/custom_bottom_sheet.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_time_period.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/closure/access_record_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/closure/access_record/access_record_controller.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/task_closure/common/common_closure_method.dart';
import 'package:enterprise_v6_app/generated/assets.dart';

/// 车辆出入
class CarAssess extends StatelessWidget {
  CarAssess({super.key});

  final controller = Get.put(AccessRecordController());

  // 报警状态
  Widget _status(int? i) {
    if (i == 0 || i == null) {
      return Container(
        padding: EdgeInsets.symmetric(vertical: 2.w, horizontal: 8.w),
        decoration: BoxDecoration(
            color: const Color(0xFF5C6DFF),
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(8.r),
              bottomRight: Radius.circular(8.r),
              topRight: Radius.circular(8.r),
            )),
        child: Text('正常',
            style: TextStyle(
              fontSize: 10.sp,
              color: Colors.white,
            )),
      );
    }
    return Container(
      padding: EdgeInsets.symmetric(vertical: 3.w, horizontal: 10.w),
      decoration: BoxDecoration(
          color: const Color(0xFFFD6B6B),
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(8.r),
            bottomRight: Radius.circular(8.r),
            topRight: Radius.circular(8.r),
          )),
      child: Text('报警($i)',
          style: TextStyle(
            fontSize: 10.sp,
            color: Colors.white,
          )),
    );
  }

  ///筛选
  Widget _screen() {
    return Container(
      width: 36.w,
      height: 32.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 18.w,
        height: 16.w,
      ),
    ).clickOnce(() {
      controller.screen(2);
    });
  }

  /// 获取右侧筛选样式
  Widget _getFilterWidget() {
    return Container(
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 20.w),
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            //渐变位置
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            //渐变颜色[始点颜色, 结束颜色]
            colors: [
              Color(0xffE7EAFF),
              Colors.white,
              Colors.white,
              Colors.white,
            ],
          ),
        ),
        child: Column(
          children: [
            Expanded(
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      Align(
                        alignment: Alignment.centerLeft,
                        child: Text(
                          '筛选',
                          style: TextStyle(
                            fontSize: 18.sp,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      ),
                      AppGaps.vGap20,
                      AppGaps.vGap16,
                      Align(
                        alignment: Alignment.centerLeft,
                        child: Text('车辆类型',style: TextStyle(
                          fontSize: 14.sp,
                          fontWeight: FontWeight.w400,
                          color: AppColors.font_color_black,
                        ),),
                      ),
                      AppGaps.vGap16,
                      Container(
                        height: 38.w,
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(8.w),
                          border: Border.all(
                            color: AppColors.font_color_gray.withOpacity(0.3),
                            width: 1,
                          ),
                        ),
                        child: Row(
                          children: [
                            Expanded(
                              child: Container(
                                padding: EdgeInsets.symmetric(horizontal: 16.w),
                                child: Text(
                                  controller.searchCarTypeShow ?? '请选择车辆类型',
                                  style: TextStyle(
                                    fontSize: 14.sp,
                                    fontWeight: FontWeight.w400,
                                    color: AppColors.font_color_black,
                                  ),
                                ),
                              ),
                            ),
                            Container(
                              width: 38.w,
                              height: 38.w,
                              alignment: Alignment.center,
                              child: SvgPicture.asset(
                                Assets.imagesIconFilterRightArrow,
                                width: 18.w,
                                height: 16.w,
                              ),
                            ),
                          ],
                        ),
                      ).click(selectedPersonType),
                      AppGaps.vGap16,
                      Align(
                        alignment: Alignment.centerLeft,
                        child: Text(
                          '培训时间',
                          style: TextStyle(
                            fontSize: 14.sp,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      ),
                      AppGaps.vGap16,
                      CustomSelectTimePeriod(
                        startTime: controller.searchStartTime ?? '',
                        endTime: controller.searchEndTime ?? '',
                        onConfirm: (start, end) {
                          controller.searchStartTime = start;
                          controller.searchEndTime = end;
                        },
                        showBorder: true,
                        iconCalendar: AppImages.icon__screen_clock,
                      ),
                    ],
                  ),
                )),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                OutlinedButton(
                  onPressed: controller.reset,
                  child: Text(
                    '重置',
                    style: TextStyle(fontSize: 16.sp, color: AppColors.font_color_blue),
                  ),
                ),
                ElevatedButton(
                    style: ButtonStyle(backgroundColor: MaterialStateProperty.all(AppColors.font_color_blue)),
                    onPressed: () {
                      controller.confirm(2);
                    },
                    child: Text(
                      '查询',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 16.sp,
                      ),
                    )),
              ],
            ),
          ],
        )
    );
  }
  // 选择车辆类型 1:普货车辆 2:社会车辆 3:小型车辆 4:应急车辆 5:其他
  selectedPersonType() async {
    CustomBottomSheet<Map<String, String>>(
      dataList: const [
        {'name': '普货车辆', 'value': '1'},
        // {'name': '社会车辆', 'value': '2'},
        {'name': '小型车辆', 'value': '3'},
        // {'name': '应急车辆', 'value': '4'},
        {'name': '其他', 'value': '5'},
      ],
      showValue: 'name',
      onConfirm: (value) {
        controller.searchCarType = value['value'];
        controller.searchCarTypeShow = value['name'];
        controller.update();
      },
    ).showBottomSheetDialog();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: controller.popRightScaffoldKeyCar,
      backgroundColor: AppColors.bg_color_grey,
      endDrawer: Container(
        width: AppDimens.sw - 80.w,
        height: AppDimens.sh,
        color: Colors.white,
        child: _getFilterWidget(),
      ),
      body: Container(
        padding: EdgeInsets.symmetric(
          horizontal: AppDimens.dp16,
        ),
        child: Column(
          children: [
            AppGaps.vGap14,
            Row(
              children: [
                Expanded(
                  child: CustomSearchEnter(
                    height: 38.w,
                    hintText: '请输入车牌号',
                    text: controller.searchCarName,
                    onCallBack: (v) {
                      controller.searchCarName = v;
                      controller.refreshPage(2);
                    },
                  ),
                ),
                AppGaps.hGap8,
                _screen(),
              ],
            ),
            AppGaps.vGap16,
            Expanded(
                child: CustomEasyRefreshList<AccessRecordEntity>(
              key: controller.globalKeyCar,
              dataLoad: (pageNum, pageSize, {search}) async {
                return controller.checkAssetsDetails(pageNum, pageSize, 0, search: search);
              },
              itemBuilder: (item, index) {
                return CustomTitleChartPage(
                  title: '车牌号码:${item.licensePlate}',
                  titleRightWidget: _status(item.numberOfViolations),
                  child: Column(
                    children: [
                      car.CustomRowTitle(
                        title: '车辆类型:',
                        content: CommonClosureMethod.getCarTypeValue(item.carType),
                      ),
                      car.CustomRowTitle(
                        title: '所属企业:',
                        content: '${item.affiliatedUnit}',
                      ),
                      car.CustomRowTitle(
                        title: '最近入企时间:',
                        content: '${item.inDate}',
                      ),
                      car.CustomRowTitle(
                        title: '最近出企时间:',
                        content: '${item.outDate}',
                      ),
                    ],
                  ),
                ).click(() {
                  controller.toDetails(item, 2);
                });
              },
            ))
          ],
        ),
      ),
    );
  }
}
